<?php
class episode_video extends Model{
	public static function getTableName(){
		return 'episode-video';
	}

	public static function getEpisodeID($video_id)
	{
	    $sql = "select episode_id from `".self::getTableName()."` where video_id='{$video_id}' order by episode_id desc";
		$ret = Model::$dbo['r']->findBySql(__class__, $sql);
		return isset($ret[0]) ? $ret[0]->episode_id :  false;
	}

	/*
	 * by donald
	 */
	public function getAll(array $params)
	{
		foreach ($params as $field => $value) {
			switch ($field) {
				case 'video_id':
					$where[] = 'video_id = ?';
					$where_array[] = $value;
					break;
			}
		}

		return self::$dbo['w']->find(__CLASS__, implode(' AND ', $where), $where_array);
	}

	/*
	 * by donald
	 */
	public function add_change_log($programme_id, $is_active = 1)
	{
		$field['type'] = $this->getTableName();
		$field['keys'] = array(
			'episode_id' => $this->episode_id,
			'video_id' => $this->video_id
		);
		$field['is_active'] = $is_active;
		$field['programme_id'] = $programme_id;
		$field['episode_id'] = 0; 
		$field['is_publish'] = 0;

		change_log::updateOne($field);
	}

	/*
	 * by donald
	 */
	public function delete_change_log()
	{
		$type = $this->getTableName();
		$keys = array(
			'episode_id' => $this->episode_id,
			'video_id' => $this->video_id
		);
		$is_publish = 0;

		change_log::delTypeAndKeysLog($type, $keys, $is_publish);
	}

}
?>
